<?php


namespace App\Model\Admin;


use EasySwoole\ORM\AbstractModel;

/**
 * table: admin_list
 * Class AdminModel
 *
 * @property $adminId
 * @property $adminName
 * @property $adminAccount
 * @property $adminPassword
 * @property $adminSession
 * @property $adminLastLoginTime
 * @property $adminLastLoginIp
 * @package App\Model\Admin
 */
class AdminModel extends AbstractModel
{
    protected $tableName = 'admin_list';

    protected $primaryKey = 'adminId';

    /**
     * @getAll
     * @keyword adminName
     * @param int $page 1
     * @param string $keyword
     * @param int $pageSize 10
     * @return array[total, list]
     */
    public function getAll($page, $keyword, $pageSize): array
    {
        $where = [];
        if (!empty($keyword)) {
            $where['adminName'] = ['%' . $keyword . '%', 'like'];
        }

        $list = $this->limit($pageSize * ($page - 1), $pageSize)
            ->order($this->primaryKey, 'Desc')
            ->withTotalCount()
            ->all($where);
        $total = $this->lastQueryResult()->getTotalCount();
        return compact('total', 'list');
    }

    /*
 * 登录成功后请返回更新后的bean
 */
    public function login(): ?AdminModel
    {
        $info = $this->get(['adminAccount' => $this->adminAccount, 'adminPassword' => $this->adminPassword]);
        return $info;
    }

    /*
     * 以account进行查询
     */
    public function accountExist($field = '*'): ?AdminModel
    {
        $info = $this->field($field)->get(['adminAccount' => $this->adminAccount]);
        return $info;
    }

    public function getOneBySession($field = '*'): ?AdminModel
    {
        $info = $this->field($field)->get(['adminSession' => $this->adminSession]);
        return $info;
    }

    public function logout()
    {
        return $this->update(['adminSession' => '']);
    }
}